package table;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.TextFilter;
import org.uqbar.arena.widgets.TextInputEvent;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;
import com.uqbar.commons.StringUtils;
import domain.Player;


public class ChangeChipsWindow extends Dialog<Player>{

	public ChangeChipsWindow(WindowOwner owner, Player model) {
		super(owner, model);
	}

	@Override
	protected void executeTask() {
	}

	@Override
	protected void addActions(Panel actionsPanel) {
		new Button(actionsPanel)
		.setCaption("change") 
		.onClick(new MessageSend(this, ACCEPT))
		.setAsDefault()
		.disableOnError();

		new Button(actionsPanel) 
		.setCaption("Cancel")
		.onClick(new MessageSend(this, CANCEL));			
	}
	
	public void change () {
		this.getModel().changeChips();
	}
	
	@Override
	protected void createFormPanel(Panel mainPanel) {
		Label betOnLabel = new Label(mainPanel);
		betOnLabel.setText("Actual chips: "+ getModel().getChips());
		
		Label change = new Label(mainPanel);
		change.setText("Change");
		TextBox chips = new TextBox(mainPanel);
		chips.bindValueToProperty(Player.CHIPS);
		TextFilter filter = new TextFilter(){
		
			public boolean accept(TextInputEvent event) {
				return (StringUtils.isBlank(event.getInputText()) || (StringUtils.isNumeric(event.getInputText())) && Integer.parseInt(event.getPotentialTextResult()) <= getModel().getMoney()) ;
			}
		}  ;
		
		chips.withFilter(filter);	
	}
	
	protected void createMainTemplate(Panel formBuilder) {
		this.setTitle(" Change Chips " );
		this.setTaskDescription(" Enter the amount of chips to change " );
		super.createMainTemplate(formBuilder);
	}
}
